<?php
/**
 * Created by PhpStorm.
 * User: 14775
 * Date: 2017/5/26
 * Time: 14:05
 */

namespace Sqapi\Behavior;

use Think\Behavior;
use Think\Log;

class APIRequestEndBehavior extends Behavior
{
    // 行为扩展的执行入口必须是run
    public function run(&$content)
    {
        if ($_REQUEST['log_api_id']) {
            $log_api_id = $_REQUEST['log_api_id'];
            try {
                $Model = M('api_log');
                $data['logID'] = $log_api_id;
                $data['responeTime'] = date('Y-m-d H:i:s');
                $data['respone_str'] = json_encode($content);
                $data['timeDiff'] = $this->getRunTime();
                $data['status'] = 1;
                $Model->data($data)->save();
            } catch (\Exception $e) {
                error_log($e->getMessage());
            }
        }
    }

    private function getRunTime()
    {
        // 显示运行时间
        G('beginTime', $GLOBALS['_beginTime']);
        G('viewEndTime');
        // 显示详细运行时间
        return G('beginTime', 'loadTime');
        //return G('beginTime','viewEndTime').'s ( Load:'.G('beginTime','loadTime').'s Init:'.G('loadTime','initTime').'s Exec:'.G('initTime','viewStartTime').'s Template:'.G('viewStartTime','viewEndTime').'s )';
    }
}